<!--
 * @Author: 杨光辉(GerhardYang)
 * @Date: 2021-10-15 10:01:32
 * @LastEditors: 杨光辉(GerhardYang)
 * @LastEditTime: 2021-10-15 10:09:01
 * @Description: file content
 * @Copyright: 超图软件华中平台客户中心 (SuperMap Software Co., Ltd. -Central China Platform)
-->
<template>
  <div />
</template>

<script>
import { updataLegend, deleteLgend } from "../../utils/mapLegend.js";
export default {
  watch: {
    checkedLayers() {
      this.updata();
    },
  },
  computed: {
    map() {
      return this.$store.state.map;
    },
    checkedLayers() {
      return this.$store.state.checkedLayers;
    },
  },
  methods: {
    jsonsort(obj, key) {
      for (var i = 0; i < obj.length; i++) {
        for (var j = i + 1; j < obj.length; j++) {
          if (obj[i][key] > obj[j][key]) {
            var temp = obj[i];
            obj[i] = obj[j];
            obj[j] = temp;
          }
        }
        // if (obj[i].childre.length) this.jsonsort(obj[i].children);
      }
      return obj;
    },
    regroup(arr, str) {
      var map = {},
        dest = [];
      arr.map((item) => {
        if (!map[item[str]]) {
          dest.push({
            maxScale: item[str],
            data: [item],
          });
          map[item[str]] = item;
        } else {
          dest
            .filter((itemD) => itemD.maxScale == item[str])[0]
            .data.push(item);
        }
      });
      return dest;
    },
    updata() {
      deleteLgend();
      if (!this.checkedLayers.length) {
        deleteLgend();
      }

      let self = this;
      this.checkedLayers.map((item) => {
        if (!item.legendinfo.length) {
          // self.$message.info(`${item.title}图层图例获取失败`);
        } else {
          let legends = [];
          this.regroup(item.legendinfo, "minScale").map((item) => {
            self.regroup(item.data, "maxScale").map((item) => {
              legends.push(item);
            });
          });
          let legend = this.jsonsort(legends, "maxScale");

          legend.length > 2 ? (legend = legend[1]) : (legend = legend[0]);
          console.log("展示图例", legend.data);
          updataLegend(item, legend.data);
        }
      });
    },
  },
};
</script>

<style></style>
